#include "DerivationTree.h"
#include "Real.h"
#include <cmath>

FloatNode::FloatNode() : NodeTree_CRTP<FloatNode>() { }

FloatNode::FloatNode(char* number, unsigned int _length) : NodeTree_CRTP<FloatNode>(), buffer(number), length(_length) { }

FloatNode::FloatNode(const FloatNode& copy) : NodeTree_CRTP<FloatNode>(copy) {
	length = copy.length;
	buffer = new char[length];
	for (unsigned int i = 0; i < length; ++i) buffer[i] = copy.buffer[i];
}

bool FloatNode::execute(Pointer<LeonardVirtualMachine> LVM) {
	long double double_number = 0;
	unsigned int j = 0;
	while (buffer[j] != '.') {
		double_number = (double_number*10) + (buffer[j] - '0');
		++j;
	}
	++j;
	unsigned int i = 1;
	while (j < length) {
		double_number = double_number + (buffer[j] - '0')/(pow(10.,static_cast<int>(i)));
		++j;
		++i;
	}
	AutoPointer<TreeExpression::Expression> nmb(new TreeExpression::Real(double_number));
	LVM->push(nmb);
	return true;
}
